package applications.alternativa3d {
	import alternativa.engine3d.containers.ConflictContainer;
	import alternativa.engine3d.core.Camera3D;
	import alternativa.engine3d.core.View;

	import jazztures.plugins.alternativa3d.Skeleton3D;

	import ui.DollModeler;

	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	
	[SWF(width=800, height=800, frameRate=30, backgroundColor=0x000000)]
	public class SkeletonModelerAlternativa3D extends Sprite
	{
		private var camera:Camera3D;
		private var camera2:Camera3D;
		
		protected var world:ConflictContainer;
		
		protected var skeleton:Skeleton3D;
	
		private var modeler:DollModeler;
		
		public function SkeletonModelerAlternativa3D() 
		{
			stage.align = StageAlign.TOP_LEFT;
			stage.scaleMode = StageScaleMode.NO_SCALE;

			addEventListener(Event.ADDED_TO_STAGE, startModeler);
		}
		
		private function startModeler(evt:Event):void{
			removeEventListener(Event.ADDED_TO_STAGE, startModeler);
			
			camera = new Camera3D();
			camera.view = new View(stage.stageWidth/2, stage.stageHeight/2);
			camera.view.x = stage.stageWidth/2;
			camera.x = camera.view.width/2;
			camera.y =  camera.view.height/2;
			camera.z = -500;
			addChild(camera.view);
			
			camera2 = new Camera3D();
			camera2.view = new View(stage.stageWidth/2, stage.stageHeight/2);
			camera2.view.x = stage.stageWidth/2;
			camera2.view.y = stage.stageHeight/2;
			camera2.y = camera2.view.height/2;
			camera2.x = -600;
			
			camera2.rotationY = Math.PI/2;
			addChild(camera2.view);
			
			world = new ConflictContainer();
			world.addChild(camera);
			world.addChild(camera2);
			
			addEventListener(Event.ENTER_FRAME, render);
			
			modeler = new DollModeler(stage.stageWidth/2, stage.stageHeight/2);
			
			buildSkeleton();
			
			addChild(modeler);
			modeler.addEventListener(Event.CHANGE, update);
		}

		protected function buildSkeleton() : void {
			skeleton = new Skeleton3D();
			world.addChild(skeleton);
		}
		
		private function update(evt:Event):void{
			skeleton.skeleton = modeler.skeleton;		
			skeleton.update();
		}

		private function render(evt:Event):void{
			camera.render();
			camera2.render();
		}
	}
}